package org.gruposp2p.dnie.server.extras;

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;

/**
 * @author jj
 * Thanks to this mail:
 * https://jersey.dev.java.net/servlets/ReadMsg?list=users&msgNo=9137
 *
 */
public class AcceptHeaderNormalizer implements ContainerRequestFilter {

    public ContainerRequest filter(ContainerRequest request) {
        String agent = request.getHeaderValue("User-Agent");
        String accept = request.getHeaderValue("Accept");
        if (agent == null || accept == null) return request;
        if (agent.contains("MSIE") && !accept.equals("application/json")) {
        request.getRequestHeaders().putSingle("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        } else if (agent.contains("Safari")) {
            request.getRequestHeaders().putSingle("Accept",accept.replace(
                    "application/xml,application/xhtml+xml,text/html;q=0.9",
                    "text/html,application/xhtml+xml,application/xml;q=0.9"));
        }
        return request;
    }

}
